/*******************************************************
  > Copyright (C) 2024 ==iot== All rights reserved.
  > File Name: 1.c
  > Author: wyf
  > Created Time: 2024年08月09日 星期五 16时02分26秒
 ***************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> //目录名获取配合DIR
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h> //文件控制操作
#include <sys/types.h> //获取pid等
#include <sys/wait.h> //等待子进程结束
#include <time.h> //时间获取
#include <sys/ipc.h> //进程通信
#include <sys/msg.h> //消息队列
#include <sys/shm.h> //共享内存
#include <signal.h> //信号处理
#include <pthread.h> //线程
struct exam
{
	int data1;
	int data2;
};

typedef struct queue
{
	struct exam content;
	struct queue *next;
}Queue;

// 初始化头结点
Queue* create_queue() {
	Queue* head = (Queue*)malloc(sizeof(Queue)); 
	head->next = NULL;  
	return head;    
}

// 入队
void push(Queue* head,struct exam *data) {
	Queue* newNode = (Queue*)malloc(sizeof(Queue)); 
	newNode->content.data1 = data->data1;  
	newNode->content.data2 = data->data2;
	Queue* p = head; 
	while (p->next != NULL) { 
		p = p->next;
	}    

	p->next = newNode;      
	newNode->next = NULL;   
	p = newNode;
}

// 获取队列头的第一个数据
int front(Queue* head) {
	if (head->next == NULL) {   
		printf("Queue is empty\n");
		return -1;           
	}
	printf("%d\n",head->next->content.data1);
	printf("%d\n",head->next->content.data2);
	return 0;
}

// 出队
void pop(Queue* head) {
	if (head->next == NULL) {   
		printf("队列是空的!\n");
		return;
	}
	Queue* p = head->next; 
	head->next = head->next->next; 
	free(p); 
}
// 返回队列的大小
int size(Queue* head) {
	int count = 0;     
	Queue* p = head->next; 
	while (p != NULL) {  
		count++;    
		p = p->next;   
	}
	return count;
}

// 判断队列是否为空
int isempty(Queue* head) {
	return head->next == NULL;  // 如果头结点的指针为空，返回1表示空；否则返回0
}

// 销毁队列
void destroy_queue(Queue* head) {
	Queue* p = head;    
	while (p != NULL) { 
		Queue* temp = p;
		p = p->next; 
		free(temp);  
	}
}
int main(int argc, const char* argv[])
{
	Queue* head = NULL;
	head = create_queue();
	int flag = 1;
	struct exam temp;
	int number = 1;
	while(flag == 1){
		memset(&temp,0,sizeof(struct exam));
		printf("请输入%d号成员的第1个数据：\t(0是退出)\n",number);

		scanf("%d",&temp.data1);
		getchar();
		if(temp.data1 == 0)
		{
			break;
		}
		printf("请输入%d号成员的第2个数据：\t(0是退出)\n",number);
		scanf("%d",&temp.data2);
		getchar();
		if(temp.data2 == 0)
		{
			break;
		}
		push(head,&temp); 
		number++;
	}
	printf("当前队列的第一个结构体的值是：\n");
	front(head);



	int i =0;
	while(1){
		printf("是否出队？\t(1是确认出队)\n");
		scanf("%d",&i);
		if(i == 1){
			pop(head);
			printf("出队后的队列中的第一个元素:\n");
			front(head);
		}
		else{
			break;
		}		
	}

	int res = 0;
	//计算队列长度
	res = size(head);
	printf("队列的长度是：%d\n",res);
	//销毁队列
	destroy_queue(head);
	printf("已销毁队列\n");


	return 0;

}


